<template>
  <div>
    <el-button @click="dialogVisible = true" :type="postCompleteType ? 'info' : btnType[this.postType]"
      :disabled="postCompleteType">{{
        postCompleteType ? btnCompleteText[this.postCompleteType] : btnText[this.postType] }}</el-button>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>{{ promptText[this.postType] }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="finish">是 的</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { getDetailInfoByPostId, completePost } from "@/api/post/post";

export default {
  name: "FinishButton",
  inject: ['reload'],
  props: {
    postId: Number,
    user: {}
  },
  data() {
    return {
      postType: 0,
      postCompleteType: 0,
      dialogVisible: false,
      btnType: ['success', 'primary'],
      btnText: ['我要上报', '我要认领'],
      btnCompleteText: ['已找到', '已认领'],
      promptText: ['您找到失物了吗？', '您确定这是您的失物吗？']
    }
  },
  methods: {
    getPostInfo() {
      getDetailInfoByPostId({
        postId: Number(this.postId)
      }).then(res => {
        if (res.data.code === 200) {
          this.postType = res.data.postDetailInfoVO.type
          this.postCompleteType = res.data.postDetailInfoVO.completeStatus
        } else {
          // this.$message.error(res.data.message)
        }
      })
    },
    finish() {
      if ([undefined, null, ''].indexOf(this.user.qqEmail) === -1) {
        completePost({
          postId: Number(this.postId),
          toUserId: Number(this.user.id)
        }).then(res => {
          if (res.data.code === 200) {
            this.dialogVisible = false
            this.reload()
            this.$notify.success({
              title: '成功',
              message: res.data.message,
            })
          } else {
            // this.$message.error(res.data.message)
          }
        })
      } else {
        this.$message.warning('您还未绑定 QQ 邮箱，请绑定 QQ 邮箱')
      }
    }
  },
  created() {
    this.user = JSON.parse(Cookies.get("user"))
    this.getPostInfo()
  }
}
</script>
